<?php

namespace App\Http\Middleware;

use Closure;

class VerifyLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $userinfo = $request->session()->get('userinfo');

        if (empty($userinfo)) {
            if ($request->ajax() || $request->wantsJson()) {
                return response()->json([
                    'code' => '400',
                    'data' => '您已退出登录或登录超时！',
                ]);
            } else {
                return redirect('/login');
            }
        }

        return $next($request);
    }
}
